package com.ccl.controller.client;

import com.ccl.entity.Article;
import com.ccl.entity.Comment;
import com.ccl.entity.Statistic;
import com.ccl.service.ArticleService;
import com.ccl.service.CommentService;
import com.ccl.service.StatisticService;
import com.ccl.utils.Commons;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.util.List;
import java.util.Map;

@Controller
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private StatisticService statisticService;
    @Autowired
    private CommentService commentService;

    @RequestMapping("/page/{pageNum}")
    public String page(HttpServletRequest request, @PathVariable("pageNum") Integer pageNum, @RequestParam(value = "count",defaultValue = "5")Integer count){
        //1.分页查询首页文章数据
        PageInfo<Article> articles = articleService.findArticleWithPage(pageNum, count);

        //2 排行榜数据
        List<Article> articleList = articleService.getHeatData();
        //保存分页数据
        request.setAttribute("articles",articles);
        //将排行榜数据保存到request域中
        request.setAttribute("articleList",articleList);
        //传递Commons对象
        //request.setAttribute("commons",new Commons());


        //跳转到首页
        return "client/index";
    }

    //进入首页
    @RequestMapping("/")
    public String index(HttpServletRequest request){
        //调用page方法
       return page(request,1,5);
    }

    //跳转到详情页
    @RequestMapping("/articleDetail/{articleId}")
    public String articleDetail(HttpServletRequest request,@PathVariable("articleId") Integer articleId, @RequestParam (value = "cp",defaultValue = "1") Integer cp,@RequestParam(value = "count",defaultValue = "5") Integer count){
        //准备详情页数据
        //1.文章信息
        Article article = articleService.findById(articleId);
        //2.评论信息
        PageInfo<Comment> comments = commentService.findCommentsByArticleId(articleId, cp, count);
        //3.根据id更新文章点击量
        statisticService.updateHits(articleId);
        //保存评论信息
        request.setAttribute("comments",comments);
        //保存文章信息
        request.setAttribute("article",article);
        //保存commons对象
        //request.setAttribute("commons",new Commons());
        return "client/articleDetails";
    }

    //跳转到登录页面
    @RequestMapping("/login")
    public String login(HttpServletRequest request, Map map){
        //保存原始访问路径
        String referer = request.getHeader("Referer");//获取原始访问路径
        String url = request.getParameter("url");//获取原始访问路径
        System.out.println("Referer"+ referer);
        System.out.println("url = " + url);

        //1. 先判断保存的url
        if (url!=null&url!=""){
            //使用url作为原始访问路径
            map.put("url",url);
        }else if (referer!=null&&referer.contains("login")){
            //2. 如果头信息中有login字段(从登录->登录)
            map.put("url","");
        }else{
            //3. 头信息作为原始访问路径
            map.put("url",referer);
        }
        //跳转到登录页面
        return "comm/login";
    }

    //处理错误页面的请求
    @RequestMapping("/errorPage/{comm}/{errnum}")
    public String errorpage(@PathVariable("comm") String comm ,@PathVariable("errnum") String errnum){

        return comm+"/"+errnum;
    }

}
